package keeto.com.vn.bms.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Collection;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.config.IBMSConfig;
import keeto.com.vn.bms.model.AndroidNotificationsModel;
import keeto.com.vn.bms.model.BMSUserModel;
import keeto.com.vn.bms.model.CommentModel;
import keeto.com.vn.bms.model.CompanyModel;
import keeto.com.vn.bms.model.DetailSergeryModel;
import keeto.com.vn.bms.model.Hospital;
import keeto.com.vn.bms.model.InfoDoctorEntity;
import keeto.com.vn.bms.model.InfoMessegerModel;
import keeto.com.vn.bms.model.ListSurgeryModel;
import keeto.com.vn.bms.model.ListUserRelatedModel;
import keeto.com.vn.bms.model.MedicalRecordModel;
import keeto.com.vn.bms.model.MsgHints;
import keeto.com.vn.bms.model.NotificationModel;
import keeto.com.vn.bms.model.Schedule;
import keeto.com.vn.bms.model.TechniciansModel;
import keeto.com.vn.bms.model.BaseInfor.DetailBMSProduct;
import keeto.com.vn.bms.model.BaseInfor.Education;
import keeto.com.vn.bms.model.BaseInfor.ProductInfor;
import keeto.com.vn.bms.model.BaseInfor.Promotion;
import keeto.com.vn.bms.model.BaseInfor.Seminar;
import keeto.com.vn.bms.model.User.EditAdminModel;
import keeto.com.vn.bms.model.report.DetailReportDoctorModel;
import keeto.com.vn.bms.model.report.DetailReportHospitalModel;
import keeto.com.vn.bms.network.request.AddTechnicalRequest;
import keeto.com.vn.bms.network.request.CreateScheduleRequest;
import keeto.com.vn.bms.network.request.DeleteMsgRequest;
import keeto.com.vn.bms.network.request.DeleteTechnicalRequest;
import keeto.com.vn.bms.network.request.EditUserInfoRequest;
import keeto.com.vn.bms.network.request.GetComDoctorRequest;
import keeto.com.vn.bms.network.request.GetComDoctorResponse;
import keeto.com.vn.bms.network.request.GetCommentRequest;
import keeto.com.vn.bms.network.request.GetDetailMessegerRequest;
import keeto.com.vn.bms.network.request.GetDetailProductBMSRequest;
import keeto.com.vn.bms.network.request.GetDetailReportByTypeRequest;
import keeto.com.vn.bms.network.request.GetDetailReportFileRequest;
import keeto.com.vn.bms.network.request.GetDetailSergeryRequest;
import keeto.com.vn.bms.network.request.GetHopitalRequest;
import keeto.com.vn.bms.network.request.GetListComHospitalRequest;
import keeto.com.vn.bms.network.request.GetListCompanyRequest;
import keeto.com.vn.bms.network.request.GetListHintsRequest;
import keeto.com.vn.bms.network.request.GetListMedicalRecordHospitalRequest;
import keeto.com.vn.bms.network.request.GetListMedicalRecordRequest;
import keeto.com.vn.bms.network.request.GetListMessegerRequest;
import keeto.com.vn.bms.network.request.GetListNoficationRequest;
import keeto.com.vn.bms.network.request.GetListProductRequest;
import keeto.com.vn.bms.network.request.GetListSurgeryRequest;
import keeto.com.vn.bms.network.request.GetMessegerTwoGroupsRequest;
import keeto.com.vn.bms.network.request.GetScheduleRequest;
import keeto.com.vn.bms.network.request.GetSupportRequest;
import keeto.com.vn.bms.network.request.GetTypeUserRequest;
import keeto.com.vn.bms.network.request.GetUserInfoRequest;
import keeto.com.vn.bms.network.request.GetUserLogonRequest;
import keeto.com.vn.bms.network.request.GetuserLogoutResquest;
import keeto.com.vn.bms.network.request.PoseNotParticipateRequest;
import keeto.com.vn.bms.network.request.SendCommentRequest;
import keeto.com.vn.bms.network.request.SendDeleteMessagerRequest;
import keeto.com.vn.bms.network.request.SendDeleteNotifyRequest;
import keeto.com.vn.bms.network.request.SendMessegerRequest;
import keeto.com.vn.bms.network.request.SendMsgtoGroupsRequest;
import keeto.com.vn.bms.network.request.SendToAdminRequest;
import keeto.com.vn.bms.network.request.SendUpdateComDoctorRequest;
import keeto.com.vn.bms.network.request.SendUpdateHospitalRecordRequest;
import keeto.com.vn.bms.network.request.SentStatusScheduleRequest;
import keeto.com.vn.bms.network.request.SetReadMessageRequest;
import keeto.com.vn.bms.network.request.SetReadNotifyRequest;
import keeto.com.vn.bms.network.request.UpdateAndroidScheduleRequest;
import keeto.com.vn.bms.network.response.AddTechnicalResponse;
import keeto.com.vn.bms.network.response.CreateScheduleResponse;
import keeto.com.vn.bms.network.response.DeleteMsgResponse;
import keeto.com.vn.bms.network.response.DeleteTechnicalReponse;
import keeto.com.vn.bms.network.response.EditUserInfoResponse;
import keeto.com.vn.bms.network.response.GetCommentResponse;
import keeto.com.vn.bms.network.response.GetDetailMessegerResponse;
import keeto.com.vn.bms.network.response.GetDetailProductBMSResponse;
import keeto.com.vn.bms.network.response.GetDetailReportFileResponse;
import keeto.com.vn.bms.network.response.GetDetailReportbyTypeResponse;
import keeto.com.vn.bms.network.response.GetDetailSergeryResponse;
import keeto.com.vn.bms.network.response.GetHopitalResponse;
import keeto.com.vn.bms.network.response.GetListComHospitalResponse;
import keeto.com.vn.bms.network.response.GetListCompanyResponse;
import keeto.com.vn.bms.network.response.GetListEducationResponse;
import keeto.com.vn.bms.network.response.GetListHintsResponse;
import keeto.com.vn.bms.network.response.GetListMedicalRecordHospitalResponse;
import keeto.com.vn.bms.network.response.GetListMedicalRecordResponse;
import keeto.com.vn.bms.network.response.GetListMessegerResponse;
import keeto.com.vn.bms.network.response.GetListNotificationResponse;
import keeto.com.vn.bms.network.response.GetListProductsResponse;
import keeto.com.vn.bms.network.response.GetListPromotionResponse;
import keeto.com.vn.bms.network.response.GetListSeminarResponse;
import keeto.com.vn.bms.network.response.GetListSurgeryResponse;
import keeto.com.vn.bms.network.response.GetMessgerTwoGroupsResponse;
import keeto.com.vn.bms.network.response.GetScheduleResponse;
import keeto.com.vn.bms.network.response.GetSupportResponse;
import keeto.com.vn.bms.network.response.GetTypeUserResponse;
import keeto.com.vn.bms.network.response.GetUserInfoResponse;
import keeto.com.vn.bms.network.response.GetUserLogonResponse;
import keeto.com.vn.bms.network.response.GetuserLogoutResponse;
import keeto.com.vn.bms.network.response.PoseNotParticipateReponse;
import keeto.com.vn.bms.network.response.SendCommentResponse;
import keeto.com.vn.bms.network.response.SendDeleteMessagerResponse;
import keeto.com.vn.bms.network.response.SendDeleteNotifyResponse;
import keeto.com.vn.bms.network.response.SendMessegerResponse;
import keeto.com.vn.bms.network.response.SendMsgtoGroupsResponse;
import keeto.com.vn.bms.network.response.SendToAdminResponse;
import keeto.com.vn.bms.network.response.SendUpdateComDoctorResponse;
import keeto.com.vn.bms.network.response.SendUpdateHospitalRecordResponse;
import keeto.com.vn.bms.network.response.SentStatusScheduleReponse;
import keeto.com.vn.bms.network.response.SetReadMessageResponse;
import keeto.com.vn.bms.network.response.SetReadNotifyResponse;
import keeto.com.vn.bms.network.response.UpdateAndroidScheduleResponse;
import keeto.com.vn.bms.network.response.getAndroidNotificationsReponse;
import keeto.com.vn.bms.utils.Utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.Html;
import android.util.Log;

public class JSONProtocol implements IBMSProtocol {
	String TAG = "JSONProtocol";
	private DefaultHttpClient httpclient;
	private Context context;
	private static final String AUTHENKEY = "AuthKey";

	public JSONProtocol(Context context, BMSCore core) {
		this.context = context;
	}

	private synchronized String getDataServerPost(String url, String data)
			throws ClientProtocolException, IOException {
		String responseStr = "";
		StringBuilder builder = new StringBuilder();
		HttpClient httpClient = new DefaultHttpClient();

		try {
			HttpPost httpPost = new HttpPost(url);
			StringEntity se = new StringEntity(data, HTTP.UTF_8);
			httpPost.setEntity(se);
			httpPost.setHeader("Content-Type",
					"application/x-www-form-urlencoded");
			httpPost.setHeader(
					"Security-Code",
					"YgO455jStiyB810F5k8zG4DicEwsYakbJBf4x1Rvnxlge+Th0PDgf/peGKUFKIUtLQm590ILDgUr10LTLSwD8A");
			HttpResponse httpRes = httpClient.execute(httpPost);
			if (httpRes.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = httpRes.getEntity();
				InputStream content = entity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(content), 8192);
				String line;
				while ((line = reader.readLine()) != null) {
					builder.append(line);
				}
				responseStr = builder.toString();
			}

			Log.d("DATASTR", "Res : " + responseStr);

		} catch (ConnectTimeoutException e) {
			Log.e("JSONProtocol 1070", e.getMessage());
			throw new ClientProtocolException();
		} catch (SocketTimeoutException e) {
			Log.e("JSONProtocol 1073", e.getMessage());
			throw new ClientProtocolException();
		} catch (SocketException e) {
			Log.e("JSONProtocol 1076", e.getMessage());
			throw new ClientProtocolException();
		} catch (UnsupportedEncodingException e) {
			Log.e("JSONProtocol 1079", e.getMessage());
			throw new ClientProtocolException();
		} catch (ClientProtocolException e) {
			Log.e("JSONProtocol 1082", e.getMessage());
			throw e;
		}
		return responseStr;

	}

	@Override
	public GetUserLogonResponse getLogon(GetUserLogonRequest request)
			throws JSONException, ClientProtocolException, IOException {
		GetUserLogonResponse response = new GetUserLogonResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_LOGON);
		requestObject.put("UserName", request.getUserName());
		requestObject.put("Password", request.getPassWord());

		Log.i(TAG,
				"Request " + METHOD_GET_LOGON + " content="
						+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());

		Log.i(TAG, "Respone " + METHOD_GET_LOGON + strResponse.toString());

		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONObject ads = responseObj.optJSONObject("data");
			BMSUserModel model = parserUserModel(ads);
			response.setUserModel(model);
		}

		return response;
	}

	@Override
	public GetuserLogoutResponse getLogout(GetuserLogoutResquest resquest)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetuserLogoutResponse response = new GetuserLogoutResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_LOGOUT);
		requestObject.put("AuthKey", resquest.getmAuthKey());

		Log.i(TAG,
				"Request " + METHOD_GET_LOGOUT + " content="
						+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());

		Log.i(TAG, "Respone " + METHOD_GET_LOGOUT + strResponse.toString());
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));

		return response;
	}

	private CompanyModel parserCompanies(JSONObject object) {
		CompanyModel company = new CompanyModel();
		String value = "";
		int intValue = 0;
		value = object.optString("ID");
		company.setComID(Utils.validateValue(value));
		value = object.optString("Name");
		company.setComName(Utils.validateValue(value));

		value = object.optString("Desc");
		company.setDesc(Utils.validateValue(value));
		intValue = object.optInt("IsActivated");
		company.setIsActivated(intValue);

		return company;
	}

	private BMSUserModel parserUserModel(JSONObject object) {
		BMSUserModel userModel = new BMSUserModel();
		String value = "";
		value = object.optString("ID");
		userModel.setUserID(Utils.validateValue(value));
		value = object.optString("UserName");
		userModel.setUserName(Utils.validateValue(value));
		value = object.optString("Fullname");
		userModel.setFullName(Utils.validateValue(value));
		value = object.optString("Email");
		userModel.setUserEmail(Utils.validateValue(value));
		value = object.optString("Group_id");
		userModel.setGroupId(Utils.validateValue(value));
		value = object.optString("Group_name");
		userModel.setGroupName(Utils.validateValue(value));
		value = object.optString("AuthKey");
		userModel.setAuthKey(Utils.validateValue(value));
		value = object.optString("Sex");
		userModel.setUserSex(Utils.validateValue(value));
		value = object.optString("Skype");
		userModel.setUserSkype(Utils.validateValue(value));
		value = object.optString("Kakao");
		userModel.setUserKakaotalk(Utils.validateValue(value));
		value = object.optString("Viber");
		userModel.setUserViber(Utils.validateValue(value));
		value = object.optString("Zalo");
		userModel.setUserZalo(Utils.validateValue(value));
		value = object.optString("Mobile");
		userModel.setUserMobile(Utils.validateValue(value));
		value = object.optString("Phone");
		userModel.setUserPhone(Utils.validateValue(value));
		value = object.optString("AdditionalInfo");
		userModel.setAdditionalInfo(Utils.validateValue(Html.fromHtml(value).toString()));
		value = object.optString("CaseGroup_name");
		userModel.setCaseGroupName(Utils.validateValue(Html.fromHtml(value).toString()));
		return userModel;

	}

	private Schedule parserSchedule(JSONObject object) {
		Schedule schedule = new Schedule();
		String value = "";
		int intValue = 0;
		intValue = object.optInt("ID");
		schedule.setId(Utils.validateIntValue(intValue));
		value = object.optString("Name");
		schedule.setName(Utils.validateValue(value));
		value = object.optString("Desc");
		schedule.setDesc(Utils.validateValue(value));
		return schedule;
	}

	private Hospital parserHospital(JSONObject object) {
		Hospital hospital = new Hospital();
		String value = "";
		int intValue = 0;
		value = object.optString("ID");
		hospital.setId(Utils.validateValue(value));
		value = object.optString("Name");
		hospital.setName(Utils.validateValue(value));
		value = object.optString("Desc");
		hospital.setDesc(Utils.validateValue(value));
		intValue = object.optInt("IsActivated");
		hospital.setIsActivated(intValue);
		return hospital;
	}

	private CommentModel parserComment(JSONObject object) {
		CommentModel commentModel = new CommentModel();
		String value = "";
		value = object.optString("Schedule_id");
		commentModel.setSchedule_id(Utils.validateValue(value));
		value = object.optString("Schedule_name");
		commentModel.setSchedule_name(Utils.validateValue(value));
		value = object.optString("Title");
		commentModel.setTitle(Utils.validateValue(value));
		value = object.optString("Content");
		commentModel.setContent(Utils.validateValue(value));
		value = object.optString("Comment_date");
		commentModel.setComment_date(Utils.validateValue(value));
		value = object.optString("User_id");
		commentModel.setUser_id(Utils.validateValue(value));
		value = object.optString("UserName");
		commentModel.setUserName(Utils.validateValue(value));
		value = object.optString("Group_id");
		commentModel.setGroup_id(Utils.validateValue(value));
		value = object.optString("Fullname");
		commentModel.setFullname(Utils.validateValue(value));

		return commentModel;
	}

	private TechniciansModel parserTechniciansModel(JSONObject object) {
		TechniciansModel techniciansModel = new TechniciansModel();
		String value = "";
		value = object.optString("ID");
		techniciansModel.setID(Utils.validateValue(value));
		value = object.optString("User_id");
		techniciansModel.setUser_id(Utils.validateValue(value));
		value = object.optString("UserName");
		techniciansModel.setUserName(Utils.validateValue(value));
		value = object.optString("Fullname");
		techniciansModel.setFullname(Utils.validateValue(value));
		value = object.optString("Status_id");
		techniciansModel.setStatus_id(Utils.validateValue(value));
		value = object.optString("Status");
		techniciansModel.setStatus(Utils.validateValue(value));
		value = object.optString("Confirmed");
		techniciansModel.setConfirmed(Utils.validateValue(value));
		value = object.optString("Assign_date");
		techniciansModel.setAssign_date(Utils.validateValue(value));
		value = object.optString("Reason");
		techniciansModel.setReason(Utils.validateValue(value));
		return techniciansModel;
	}

	private ListUserRelatedModel parserListUserRelatedModel(JSONObject object) {
		ListUserRelatedModel listUserRelatedModel = new ListUserRelatedModel();
		String value = "";
		value = object.optString("User_id");
		listUserRelatedModel.setUser_id(Utils.validateValue(value));
		value = object.optString("UserName");
		listUserRelatedModel.setUserName(Utils.validateValue(value));
		value = object.optString("Fullname");
		listUserRelatedModel.setFullname(Utils.validateValue(value));
		value = object.optString("Activity_id");
		listUserRelatedModel.setActivity_id(Utils.validateValue(value));
		value = object.optString("Activity");
		listUserRelatedModel.setActivity(Utils.validateValue(value));
		value = object.optString("Schedule_role_id");
		listUserRelatedModel.setSchedule_role_id(Utils.validateValue(value));
		value = object.optString("Schedule_role");
		listUserRelatedModel.setSchedule_role(Utils.validateValue(value));

		return listUserRelatedModel;
	}

	private DetailSergeryModel parserDetailSergeryModel(JSONObject object) {
		DetailSergeryModel itemDetail = new DetailSergeryModel();
		String value = "";
		value = object.optString("ID");
		itemDetail.setID(Utils.validateValue(value));
		value = object.optString("Code");
		itemDetail.setCode(Utils.validateValue(value));
		value = object.optString("Name");
		itemDetail.setName(Utils.validateValue(value));
		value = object.optString("Price");
		itemDetail.setPrice(Utils.validateValue(value));
		value = object.optString("Created_date");
		itemDetail.setCreated_date(Utils.validateValue(value));
		value = object.optString("Updated_date");
		itemDetail.setUpdated_date(Utils.validateValue(value));
		value = object.optString("Hospital_id");
		itemDetail.setHospital_id(Utils.validateValue(value));
		value = object.optString("Hospital_name");
		itemDetail.setHospital_name(Utils.validateValue(value));
		value = object.optString("Require");
		itemDetail.setRequire(Utils.validateValue(value));
		value = object.optString("Address");
		itemDetail.setAddress(Utils.validateValue(value));
		value = object.optString("Level");
		itemDetail.setLevel(Utils.validateValue(value));
		value = object.optString("User_id");
		itemDetail.setUser_id(Utils.validateValue(value));
		value = object.optString("AddInfos");
		itemDetail.setAddInfos(Utils.validateValue(value));
		value = object.optString("Execute_date");
		itemDetail.setExecute_date(Utils.validateValue(value));
		value = object.optString("TypeCost");
		itemDetail.setTypeCost(Utils.validateValue(value));
		value = object.optString("UserName");
		itemDetail.setUserName(Utils.validateValue(value));
		value = object.optString("Fullname");
		itemDetail.setFullname(Utils.validateValue(value));
		value = object.optString("SchedStt_id");
		itemDetail.setSchedStt_id(Utils.validateValue(value));
		value = object.optString("SchedStatus");
		itemDetail.setSchedStatus(Utils.validateValue(value));
		value = object.optString("Case_id");
		itemDetail.setCase_id(Utils.validateValue(value));
		value = object.optString("Case_name");
		itemDetail.setCase_name(Utils.validateValue(value));
		return itemDetail;
	}

	@Override
	public CreateScheduleResponse createScheldule(CreateScheduleRequest request)
			throws JSONException, ClientProtocolException, IOException {
		CreateScheduleResponse response = new CreateScheduleResponse();
		JSONObject resquestObject = new JSONObject();
		resquestObject.put("actionType", METHOD_CREATE_SCHEDULE);
		resquestObject.put(AUTHENKEY, request.getAuthenKey());
		resquestObject.put("Code", request.getCode());
		resquestObject.put("Name", request.getName());
		resquestObject.put("Execute_date", request.getExecute_date());
		resquestObject.put("Price", request.getPrice());
		resquestObject.put("Hospital_id", request.getHospital_id());
		resquestObject.put("Address", request.getAddress());
		resquestObject.put("Require", request.getRequire());
		resquestObject.put("Level", request.getLevel());
		resquestObject.put("Case_id", request.getCase_id());
		resquestObject.put("AddInfos", request.getAddInfos());
		resquestObject.put("TypeCost", request.getTypeCost());
		resquestObject.put("SchedUserRole_id", request.getSchedUserRole_id());
		resquestObject.put("User_id", request.getUser_id());
		Log.e(TAG + " " + METHOD_CREATE_SCHEDULE,
				"content=" + resquestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + resquestObject.toString());
		Log.e(TAG + " " + METHOD_CREATE_SCHEDULE, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));
		return response;
	}

	@Override
	public UpdateAndroidScheduleResponse updateAndroidSchedule(
			UpdateAndroidScheduleRequest request) throws JSONException,
			ClientProtocolException, IOException {
		UpdateAndroidScheduleResponse response = new UpdateAndroidScheduleResponse();
		JSONObject resquestObject = new JSONObject();
		resquestObject.put("actionType", METHOD_UPDATE_ANDROID_SCHEDULE);
		resquestObject.put(AUTHENKEY, request.getAuthKey());
		resquestObject.put("Code", request.getCode());
		resquestObject.put("Name", request.getName());
		resquestObject.put("Execute_date", request.getExecute_date());
		resquestObject.put("Price", request.getPrice());
		resquestObject.put("Hospital_id", request.getHospital_id());
		resquestObject.put("Address", request.getAddress());
		resquestObject.put("Require", request.getRequire());
		resquestObject.put("Level", request.getLevel());
		resquestObject.put("Case_id", request.getCase_id());
		resquestObject.put("AddInfos", request.getAddInfos());
		resquestObject.put("TypeCost", request.getTypeCost());
		resquestObject.put("Price", request.getPrice());
		resquestObject.put("ID", request.getID());
		resquestObject.put("SchedUserRole_id", request.getSchedUserRole_id());
		resquestObject.put("User_id", request.getUser_id());
		Log.e("====TAG====Name, Price, Require, Execute_date",
				request.getName() + " - " + request.getPrice() + " - "
						+ request.getRequire() + " - "
						+ request.getExecute_date());
		Log.e(TAG + " " + METHOD_UPDATE_ANDROID_SCHEDULE, "content="
				+ resquestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + resquestObject.toString());
		Log.e(TAG + " " + METHOD_UPDATE_ANDROID_SCHEDULE, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));
		return response;
	}

	@Override
	public GetTypeUserResponse getTypeUser(GetTypeUserRequest request)
			throws JSONException, ClientProtocolException, IOException {
		GetTypeUserResponse response = new GetTypeUserResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_TYPE_USER);
		requestObject.put(AUTHENKEY, request.getAuthenKey());
		requestObject.put("Group_id", request.getGroupId());
		requestObject.put("pageLimit", request.getPageLimit());
		requestObject.put("pageNumber", request.getPageNumber());
		
		Log.e(TAG + " " + METHOD_GET_TYPE_USER,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + " " + METHOD_GET_TYPE_USER, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setGroupID(request.getGroupId());
		response.setResultCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));
		ArrayList<BMSUserModel> listUserModel = new ArrayList<BMSUserModel>();
		if (response.getResultCode() == IBMSProtocol.STATUS_CODE_OK) {
			response.setCountPage(responseObj.getInt("countPage"));
			response.setRecordNumber(responseObj.getInt("recordNumber"));
			JSONArray jsonArray = responseObj.getJSONArray("data");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject ads = jsonArray.getJSONObject(i);
				BMSUserModel model = parserUserModel(ads);
				listUserModel.add(model);
			}
			response.setListUserModel(listUserModel);
		}
		return response;
	}

	@Override
	public GetUserInfoResponse getUserInfo(GetUserInfoRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetUserInfoResponse response = new GetUserInfoResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_USERINFO);
		requestObject.put("AuthKey", request.getAuthKey());

		Log.i(TAG, "Request " + METHOD_GET_USERINFO + " content="
				+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.i(TAG, "Respone " + METHOD_GET_USERINFO + strResponse.toString());

		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONObject ads = responseObj.optJSONObject("data");
			BMSUserModel model = parserUserModel(ads);
			response.setUserModel(model);
		}
		return response;
	}

	@Override
	public EditUserInfoResponse editUserInfo(EditUserInfoRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		EditUserInfoResponse response = new EditUserInfoResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_EDIT_USERINFO);
		requestObject.put("AuthKey", request.getAuthKey());
		if (request.getUserModel().getFullname() != null) {
			requestObject.put("Fullname", request.getUserModel().getFullname());
		}
		if (request.getUserModel().getPhone() != null) {
			requestObject.put("Phone", request.getUserModel().getPhone());
		}
		if (request.getUserModel().getEmail() != null) {
			requestObject.put("Email", request.getUserModel().getEmail());
		}
		Log.i(TAG, "Request " + METHOD_GET_EDIT_USERINFO + " content="
				+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.i(TAG,
				"Respone " + METHOD_GET_EDIT_USERINFO + strResponse.toString());
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResuiltMsg(responseObj.getString("resultMsg"));

		return response;
	}

	@Override
	public GetListSurgeryResponse getListSurgery(GetListSurgeryRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetListSurgeryResponse response = new GetListSurgeryResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_LIST_SURGERY);
		requestObject.put("AuthKey", request.getAuthKey());
		if (request.getSortby() > 0) {
			requestObject.put("SortBy", request.getSortby());
		}
		if (request.getGroupby() > 0) {
			requestObject.put("GroupBy", request.getGroupby());
			requestObject.put("ObjectID", request.getObjID());
		}
		requestObject.put("pageLimit", request.getPagelimit());
		requestObject.put("pageNumber", request.getPagenumber());

		Log.i(TAG, "Request " + METHOD_GET_LIST_SURGERY + " content="
				+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.i(TAG,
				"Respone " + METHOD_GET_LIST_SURGERY + strResponse.toString());
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));

		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			response.setCountPage(responseObj.getInt("countPage"));
			JSONArray ads = responseObj.optJSONArray("data");
			if (ads == null) {
				return response;
			}
			ArrayList<ListSurgeryModel> arrayList = new ArrayList<ListSurgeryModel>();
			for (int i = 0; i < ads.length(); i++) {
				JSONObject adJO = ads.optJSONObject(i);
				ListSurgeryModel model = parserListSurgeryModel(adJO);
				arrayList.add(model);
			}
			response.setListSurgeryModels(arrayList);
		}

		return response;
	}

	private ListSurgeryModel parserListSurgeryModel(JSONObject object) {
		ListSurgeryModel model = new ListSurgeryModel();
		String value = "";
		value = object.optString("ID");
		model.setSurgeryId(Utils.validateValue(value));
		value = object.optString("Name");
		model.setSurgeryName(Utils.validateValue(value));
		value = object.optString("Price");
		model.setSurgeryprice(Utils.validateValue(value));
		value = object.optString("Created_date");
		model.setCreated_date(Utils.validateValue(value));
		value = object.optString("Updated_date");
		model.setUpdate_date(Utils.validateValue(value));
		value = object.optString("Hospital_id");
		model.setHospitalId(Utils.validateValue(value));
		value = object.optString("Hospital_name");
		model.setHospitalname(Utils.validateValue(value));
		value = object.optString("Require");
		model.setRequire(Utils.validateValue(value));
		value = object.optString("Address");
		model.setAddress(Utils.validateValue(value));
		value = object.optString("Level");
		model.setLever(Utils.validateValue(value));
		value = object.optString("User_id");
		model.setUserId(Utils.validateValue(value));
		value = object.optString("AddInfos");
		model.setAddinfo(Utils.validateValue(value));
		value = object.optString("Execute_date");
		model.setExecute_date(Utils.validateValue(value));
		value = object.optString("UserName");
		model.setUsername(Utils.validateValue(value));
		value = object.optString("Fullname");
		model.setFullname(Utils.validateValue(value));
		value = object.optString("SchedStatus");
		model.setSurgeryStatus(Utils.validateValue(value));
		value = object.optString("SchedStt_id");
		model.setSurgerySttId(Utils.validateValue(value));
		value = object.optString("Case_id");
		model.setCaseID(Utils.validateValue(value));
		value = object.optString("Case_name");
		model.setCaseName(Utils.validateValue(value));

		return model;

	}

	@Override
	public GetScheduleResponse getSchedule(GetScheduleRequest request)
			throws JSONException, ClientProtocolException, IOException {
		GetScheduleResponse response = new GetScheduleResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_SCHEDULE);
		requestObject.put(AUTHENKEY, request.getAuthenKey());
		Log.e(TAG + " " + METHOD_GET_SCHEDULE,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + " " + METHOD_GET_SCHEDULE, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));
		ArrayList<Schedule> listSchedule = new ArrayList<Schedule>();
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONArray jsonArray = responseObj.getJSONArray("data");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject obj = jsonArray.getJSONObject(i);// .getJSONObject("CaseGroup");
				Schedule schedule = parserSchedule(obj);
				listSchedule.add(schedule);
			}
			response.setListSchedule(listSchedule);
		}
		return response;
	}

	@Override
	public GetHopitalResponse getHopital(GetHopitalRequest request)
			throws JSONException, ClientProtocolException, IOException {
		GetHopitalResponse response = new GetHopitalResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_HOSPITAL);
		requestObject.put(AUTHENKEY, request.getAuthenKey());
		Log.e(TAG + " " + METHOD_GET_HOSPITAL,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + " " + METHOD_GET_HOSPITAL, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));
		ArrayList<Hospital> listHospitals = new ArrayList<Hospital>();
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONArray jsonArray = responseObj.getJSONArray("data");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject obj = jsonArray.getJSONObject(i);
				Hospital hospital = parserHospital(obj);
				listHospitals.add(hospital);
			}
			response.setListHospital(listHospitals);
		}
		return response;
	}

	@Override
	public GetCommentResponse getComment(GetCommentRequest request)
			throws JSONException, ClientProtocolException, IOException {
		GetCommentResponse response = new GetCommentResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_COMMENT);
		requestObject.put(AUTHENKEY, request.getAuthenKey());
		requestObject.put("Schedule_id", request.getSchedule_id());
		requestObject.put("pageLimit", request.getPageLimit());
		requestObject.put("pageNumber", request.getPageNumber());
		Log.e(TAG + " " + METHOD_GET_COMMENT,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + " " + METHOD_GET_COMMENT, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));

		ArrayList<CommentModel> listComment = new ArrayList<CommentModel>();
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			response.setCountPage(responseObj.getInt("countPage"));
			response.setRecordNumber(responseObj.getInt("recordNumber"));
			JSONArray jsonArray = responseObj.getJSONArray("data");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject obj = jsonArray.getJSONObject(i);
				CommentModel commentModel = parserComment(obj);
				listComment.add(commentModel);
			}
			response.setListComment(listComment);
		}
		return response;
	}

	@Override
	public GetDetailSergeryResponse getDetailSergery(
			GetDetailSergeryRequest request) throws JSONException,
			ClientProtocolException, IOException {
		GetDetailSergeryResponse reponse = new GetDetailSergeryResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_ANDROID_SCHEDULE);
		requestObject.put(AUTHENKEY, request.getAuthenKey());
		requestObject.put("ID", request.getId());
		Log.e(TAG + " " + METHOD_GET_ANDROID_SCHEDULE, "content="
				+ requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + " " + METHOD_GET_ANDROID_SCHEDULE, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		reponse.setReturnCode(responseObj.getInt("resultCode"));
		reponse.setResultMsg(responseObj.getString("resultMsg"));
		DetailSergeryModel itemDetail = new DetailSergeryModel();
		ArrayList<TechniciansModel> itemDetailTechniciansModel = new ArrayList<TechniciansModel>();
		ArrayList<ListUserRelatedModel> itemDetailListUserRelatedModel = new ArrayList<ListUserRelatedModel>();
		if (reponse.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONObject jsonObject = responseObj.getJSONObject("data");
			itemDetail = parserDetailSergeryModel(jsonObject);
			reponse.setItemDetail(itemDetail);
			JSONArray jsonArrayTechnicians = jsonObject
					.getJSONArray("Technicians");
			JSONArray jsonArrayListUserRelated = jsonObject
					.getJSONArray("ListUserRelated");
			for (int i = 0; i < jsonArrayTechnicians.length(); i++) {
				JSONObject jsonObjectTechnicians = jsonArrayTechnicians
						.getJSONObject(i);
				TechniciansModel item = parserTechniciansModel(jsonObjectTechnicians);
				itemDetailTechniciansModel.add(item);
			}
			for (int i = 0; i < jsonArrayListUserRelated.length(); i++) {
				JSONObject jsonObjectListUserRelated = jsonArrayListUserRelated
						.getJSONObject(i);
				ListUserRelatedModel item = parserListUserRelatedModel(jsonObjectListUserRelated);
				itemDetailListUserRelatedModel.add(item);
			}
			reponse.setItemDetailTechniciansModel(itemDetailTechniciansModel);
			reponse.setItemDetailListUserRelatedModel(itemDetailListUserRelatedModel);
		}
		return reponse;
	}

	@Override
	public AddTechnicalResponse addTechnical(AddTechnicalRequest request)
			throws JSONException, ClientProtocolException, IOException {
		AddTechnicalResponse reponse = new AddTechnicalResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_ADD_TECHNICAL);
		requestObject.put(AUTHENKEY, request.getAuthKey());
		requestObject.put("Schedule_id", request.getSchedule_id());
		requestObject.put("User_id", request.getUser_id());
		Log.e(TAG + " " + METHOD_ADD_TECHNICAL,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + " " + METHOD_ADD_TECHNICAL, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		reponse.setReturnCode(responseObj.getInt("resultCode"));
		reponse.setResultMsg(responseObj.getString("resultMsg"));
		// reponse.setScheduleTechnicianID(responseObj //Kien xoa truong nay vi
		// trong response khong ton tai chuoi ScheduleTechnicianID nua
		// .getString("ScheduleTechnicianID"));
		return reponse;
	}

	@Override
	public SentStatusScheduleReponse sentStatusSchedule(
			SentStatusScheduleRequest request) throws JSONException,
			ClientProtocolException, IOException {
		SentStatusScheduleReponse response = new SentStatusScheduleReponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_CHANGE_STATUSSCHEDULE);
		requestObject.put(AUTHENKEY, request.getAuthKey());
		requestObject.put("ID", request.getID());
		requestObject.put("Status", request.getStatus());
		Log.e(TAG + " " + METHOD_CHANGE_STATUSSCHEDULE, "content="
				+ requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + " " + METHOD_CHANGE_STATUSSCHEDULE, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));
		response.setStatus(request.getStatus()); // lay ve status de xu ly
		// Listener
		return response;
	}

	@Override
	public GetListCompanyResponse getListCompanies(GetListCompanyRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetListCompanyResponse response = new GetListCompanyResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_COMPANY);
		requestObject.put(AUTHENKEY, request.getAuthenKey());
		Log.e(TAG + "" + METHOD_GET_COMPANY,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + " " + METHOD_GET_COMPANY, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));
		ArrayList<CompanyModel> listComs = new ArrayList<CompanyModel>();
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONArray jsonArray = responseObj.getJSONArray("data");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject obj = jsonArray.getJSONObject(i);
				CompanyModel company = parserCompanies(obj);
				listComs.add(company);
			}
			response.setListComs(listComs);
		}
		return response;

	}

	private MedicalRecordModel parserRecords(JSONObject obj) {
		// TODO Auto-generated method stub
		String value;
		int intvalue;
		MedicalRecordModel record = new MedicalRecordModel();
		value = obj.optString("Name");
		record.setNameRecord(Utils.validateValue(value));
		value = obj.optString("Desc");
		record.setDescription(value);
		value = obj.optString("ID");
		record.setId(Utils.validateValue(value));

		return record;
	}

	@Override
	public GetListMedicalRecordResponse getListRecord(
			GetListMedicalRecordRequest request) throws JSONException,
			ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetListMedicalRecordResponse response = new GetListMedicalRecordResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_RECORD);
		requestObject.put(AUTHENKEY, request.getAuthenKey());
		Log.e(TAG + "" + METHOD_GET_RECORD,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + " " + METHOD_GET_RECORD, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));
		ArrayList<MedicalRecordModel> listRecords = new ArrayList<MedicalRecordModel>();
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONArray jsonArray = responseObj.getJSONArray("data");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject obj = jsonArray.getJSONObject(i);
				MedicalRecordModel record = parserRecords(obj);
				listRecords.add(record);
			}
			response.setRecords(listRecords);
		}

		return response;
	}

	@Override
	public SendCommentResponse sentComment(SendCommentRequest request)
			throws JSONException, ClientProtocolException, IOException {
		SendCommentResponse response = new SendCommentResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_SEND_COMMENT);
		requestObject.put(AUTHENKEY, request.getAuthKey());
		requestObject.put("Schedule_id", request.getSchedule_id());
		requestObject.put("Content", request.getContent());
		requestObject.put("Title", request.getTitle());
		requestObject.put("Comment_date", request.getComment_date());
		Log.e(TAG + " " + METHOD_SEND_COMMENT,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + " " + METHOD_SEND_COMMENT, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));
		return response;
	}

	@Override
	public GetListNotificationResponse getListNotify(
			GetListNoficationRequest request) throws JSONException,
			ClientProtocolException, IOException {
		GetListNotificationResponse response = new GetListNotificationResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_LIST_NOTIFICATION);
		requestObject.put("AuthKey", request.getAuthkey());
		requestObject.put("pageLimit", request.getPageLimit());
		requestObject.put("pageNumber", request.getPageNumber());

		Log.i(TAG, "Request " + METHOD_GET_LIST_NOTIFICATION + " content="
				+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.i(TAG,
				"Respone " + METHOD_GET_LIST_NOTIFICATION
				+ strResponse.toString());
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));

		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			response.setPageCount(responseObj.getInt("countPage"));
			JSONArray ads = responseObj.optJSONArray("data");
			if (ads == null) {
				return response;
			}
			ArrayList<NotificationModel> arrayList = new ArrayList<NotificationModel>();
			for (int i = 0; i < ads.length(); i++) {
				JSONObject adJO = ads.optJSONObject(i);
				NotificationModel model = parserNotificationModel(adJO);
				arrayList.add(model);
			}
			response.setArrayList(arrayList);
		}

		return response;
	}

	private NotificationModel parserNotificationModel(JSONObject object) {
		NotificationModel model = new NotificationModel();
		String value = "";
		value = object.optString("ID");
		model.setNotifyId(Utils.validateValue(value));
		value = object.optString("Activity_id");
		model.setActivitiId(Utils.validateValue(value));
		value = object.optString("Activity");
		model.setActivitiTitle(Utils.validateValue(value));
		value = object.optString("Message");
		model.setNotifyContent(Utils.validateValue(value));
		value = object.optString("Object_id");
		model.setObjId(Utils.validateValue(value));
		value = object.optString("Activity_date");
		model.setNotifyTime(Utils.validateValue(value));
		value = object.optString("User_id");
		model.setUserID(Utils.validateValue(value));
		value = object.optString("Fullname");
		model.setUserFullname(Utils.validateValue(value));
		value = object.optString("Read");
		if (Utils.validateValue(value).equals("0")){
			model.setRead(true);
		}else{
			model.setRead(false);
		}
		value = object.optString("Fullname");
		model.setUserFullname(Utils.validateValue(value));
		return model;

	}

	@Override
	public GetListMessegerResponse getListMesseger(
			GetListMessegerRequest request) throws JSONException,
			ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetListMessegerResponse response = new GetListMessegerResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_LIST_MESSEGER);
		requestObject.put("AuthKey", request.getAuthKey());
		requestObject.put("pageLimit", request.getPageLimit());
		requestObject.put("pageNumber", request.getPageNumber());
		requestObject.put("Type", request.getType());

		Log.i(TAG, "Request " + METHOD_GET_LIST_MESSEGER + " content="
				+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.i(TAG,
				"Respone " + METHOD_GET_LIST_MESSEGER + strResponse.toString());
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));

		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			response.setPageCount(responseObj.getInt("countPage"));
			JSONArray ads = responseObj.optJSONArray("data");
			if (ads == null) {
				return response;
			}
			ArrayList<InfoMessegerModel> arrayList = new ArrayList<InfoMessegerModel>();
			for (int i = 0; i < ads.length(); i++) {
				JSONObject adJO = ads.optJSONObject(i);
				InfoMessegerModel model = parserMessegerModel(adJO);
				arrayList.add(model);
			}
			response.setListMsg(arrayList);
		}

		return response;
	}

	private InfoMessegerModel parserMessegerModel(JSONObject object) {
		InfoMessegerModel model = new InfoMessegerModel();
		String value = "";

		value = object.optString("ID");
		model.setMsgID(Utils.validateValue(value));
		value = object.optString("User_id");
		model.setUserID(Utils.validateValue(value));
		value = object.optString("UserName");
		model.setUserName(Utils.validateValue(value));
		value = object.optString("Title");
		model.setMsgtittle(Utils.validateValue(value));
		value = object.optString("Content");
		model.setMsgContent(Utils.validateValue(value));
		value = object.optString("Date");
		model.setMsgTime(Utils.validateValue(value));
		value = object.optString("ToUser_id");
		model.setToUserId(Utils.validateValue(value));
		value = object.optString("ToUserName");
		model.setToUserName(Utils.validateValue(value));
		value = object.optString("Fullname");
		model.setFullName(Utils.validateValue(value));
		value = object.optString("ToFullName");
		model.setToUserName(Utils.validateValue(value));
		value = object.optString("Read");
		if (Utils.validateValue(value).equals("0")){
			model.setRead(true);
		}else{
			model.setRead(false);
		}
		return model;
	}

	@Override
	public GetDetailMessegerResponse getDetailMesseger(
			GetDetailMessegerRequest request) throws JSONException,
			ClientProtocolException, IOException {
		// TODO Auto-generated method stub

		GetDetailMessegerResponse response = new GetDetailMessegerResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_DETAIL_MESSEGER);
		requestObject.put("AuthKey", request.getAuthkey());
		requestObject.put("ID", request.getMsgId());
		requestObject.put("pageLimit", request.getPageLimit());
		requestObject.put("pageNumber", request.getPageNumber());

		Log.i(TAG, "Request " + METHOD_GET_DETAIL_MESSEGER + " content="
				+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.i(TAG,
				"Respone " + METHOD_GET_DETAIL_MESSEGER
				+ strResponse.toString());
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));

		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			response.setPageCount(responseObj.getInt("countPage"));
			JSONArray ads = responseObj.optJSONArray("data");
			if (ads == null) {
				return response;
			}
			ArrayList<InfoMessegerModel> arrayList = new ArrayList<InfoMessegerModel>();
			for (int i = 0; i < ads.length(); i++) {
				JSONObject adJO = ads.optJSONObject(i);
				InfoMessegerModel model = parserMessegerModel(adJO);
				arrayList.add(model);
			}
			response.setListMsg(arrayList);
		}

		return response;
	}

	@Override
	public SendMessegerResponse sendMesseger(SendMessegerRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		SendMessegerResponse response = new SendMessegerResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_SEND_MESSEGER);
		requestObject.put("AuthKey", request.getAuthKey());
		requestObject.put("User_id", request.getUserId());
		requestObject.put("Content", request.getContent());
		requestObject.put("Group_id", request.getGroupId());
		if (request.getTittle() != null && !request.getTittle().equals("")) {
			requestObject.put("Title", request.getTittle());
		}
		Log.i(TAG, "Request " + METHOD_SEND_MESSEGER + " content="
				+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.i(TAG, "Respone " + METHOD_SEND_MESSEGER + strResponse.toString());
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResuiltMsg(responseObj.getString("resultMsg"));
		response.setIdMsg(responseObj.getString("ID"));

		return response;
	}

	@Override
	public DeleteTechnicalReponse getDeleteTechnical(
			DeleteTechnicalRequest request) throws JSONException,
			ClientProtocolException, IOException {
		DeleteTechnicalReponse reponse = new DeleteTechnicalReponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_DELETE_TECHNICAL);
		requestObject.put("AuthKey", request.getAuthKey());
		requestObject.put("ID", request.getID());
		// requestObject.put("Reason", request.getReason());
		Log.e(TAG + " " + METHOD_DELETE_TECHNICAL,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + " " + METHOD_DELETE_TECHNICAL, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		reponse.setReturnCode(responseObj.getInt("resultCode"));
		reponse.setResultMsg(responseObj.getString("resultMsg"));
		return reponse;
	}

	@Override
	public GetListEducationResponse getListCourses(GetListProductRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetListEducationResponse response = new GetListEducationResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_LIST_COURSES);
		requestObject.put("pageLimit", request.getPageLimit());
		requestObject.put("pageNumber", request.getPageNumber());
		Log.v(TAG + "" + METHOD_GET_LIST_COURSES, "content=" + ""
				+ requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.v(TAG + " " + METHOD_GET_LIST_COURSES, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setPageCount(responseObj.getInt("countPage"));
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONArray ads = responseObj.optJSONArray("data");
			if (ads == null) {
				return response;
			}
			ArrayList<Education> arrayList = new ArrayList<Education>();
			for (int i = 0; i < ads.length(); i++) {
				JSONObject adJO = ads.optJSONObject(i);
				Education model = parserCourses(adJO);
				arrayList.add(model);
			}
			response.setListEducation(arrayList);
		}
		return response;
	}

	private Education parserCourses(JSONObject obj) {
		// TODO Auto-generated method stub
		Education education = new Education();
		String value = "";
		value = obj.optString("ID");
		education.setID(Utils.validateValue(value));

		value = obj.optString("Name");
		education.setName(Utils.validateValue(value));

		value = obj.optString("Title");
		education.setTitle(Utils.validateValue(value));

		value = obj.optString("Intro");
		education.setIntro(Utils.validateValue(value));

		value = obj.optString("Desc");
		education.setDesc(Utils.validateValue(value));

		value = obj.optString("Image");
		education.setImage(Utils.validateValue(value));

		value = obj.optString("Created_date");
		education.setCreateTime(Utils.validateValue(value));

		value = obj.optString("Duration_from");
		education.setDurationFrom(Utils.validateValue(value));

		value = obj.optString("Duration_to");
		education.setDurationTo(Utils.validateValue(value));
		return education;
	}

	@Override
	public PoseNotParticipateReponse getPoseNotParticipate(
			PoseNotParticipateRequest request) throws JSONException,
			ClientProtocolException, IOException {
		PoseNotParticipateReponse reponse = new PoseNotParticipateReponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_CHANGE_TECHNICAL);
		requestObject.put("AuthKey", request.getUserAuthKey());
		requestObject.put("ID", request.getiD());
		requestObject.put("Reason", request.getReason());
		requestObject.put("Status_id", request.getStatus_id());
		Log.e(TAG + " " + METHOD_CHANGE_TECHNICAL,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + " " + METHOD_CHANGE_TECHNICAL, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		reponse.setReturnCode(responseObj.getInt("resultCode"));
		reponse.setResultMsg(responseObj.getString("resultMsg"));
		return reponse;
	}

	@Override
	public GetListPromotionResponse getListPromos(GetListProductRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetListPromotionResponse response = new GetListPromotionResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_LIST_PROMOS);
		requestObject.put("pageLimit", request.getPageLimit());
		requestObject.put("pageNumber", request.getPageNumber());
		Log.e(TAG + METHOD_GET_LIST_PROMOS,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + METHOD_GET_LIST_PROMOS, strResponse);
		JSONObject responseObject = new JSONObject(strResponse);
		response.setReturnCode(responseObject.getInt("resultCode"));
		response.setCountPage(responseObject.getInt("countPage"));
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONArray jArray = responseObject.optJSONArray("data");
			if (jArray == null) {
				return response;
			}
			ArrayList<Promotion> list = new ArrayList<Promotion>();
			for (int i = 0; i < jArray.length(); i++) {
				JSONObject obj = jArray.optJSONObject(i);
				Promotion promo = parsePromos(obj);
				list.add(promo);
			}
			response.setListCourses(list);
		}

		return response;
	}

	private Promotion parsePromos(JSONObject obj) {
		// TODO Auto-generated method stub
		Promotion promo = new Promotion();
		String value = "";
		value = obj.optString("ID");
		promo.setID(Utils.validateValue(value));
		value = obj.optString("Title");
		promo.setTitle(Utils.validateValue(value));
		value = obj.optString("Name");
		promo.setName(Utils.validateValue(value));
		value = obj.optString("Intro");
		promo.setIntro(Utils.validateValue(value));
		value = obj.optString("Desc");
		promo.setDesc(Utils.validateValue(value));
		value = obj.optString("Image");
		promo.setImage(Utils.validateValue(value));
		value = obj.optString("Duration_from");
		promo.setDurationFrom(Utils.validateValue(value));
		value = obj.optString("Created_date");
		promo.setCreateTime(Utils.validateValue(value));
		value = obj.optString("Duration_to");
		promo.setDurationTo(Utils.validateValue(value));
		return promo;
	}

	@Override
	public GetListProductsResponse getListProducts(GetListProductRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetListProductsResponse response = new GetListProductsResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_LIST_PRODUCTS);
		requestObject.put("pageLimit", request.getPageLimit());
		requestObject.put("pageNumber", request.getPageNumber());

		Log.e(TAG + METHOD_GET_LIST_PRODUCTS,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + METHOD_GET_LIST_PRODUCTS, strResponse);
		JSONObject obj = new JSONObject(strResponse);
		response.setReturnCode(obj.getInt("resultCode"));
		response.setCountPage(obj.getInt("countPage"));
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONArray jArray = obj.optJSONArray("data");
			ArrayList<ProductInfor> products = new ArrayList<ProductInfor>();
			for (int i = 0; i < jArray.length(); i++) {
				JSONObject object = jArray.optJSONObject(i);
				ProductInfor product = parserProduct(object);
				products.add(product);
			}
			response.setListProducts(products);
		}

		return response;
	}

	@Override
	public getAndroidNotificationsReponse getAndroidNotifications(
			String authenKey) throws JSONException, ClientProtocolException,
			IOException {
		getAndroidNotificationsReponse reponse = new getAndroidNotificationsReponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_NOTIFICATION);
		requestObject.put("AuthKey", authenKey);
		Log.e(TAG + METHOD_GET_LIST_PRODUCTS,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + METHOD_GET_LIST_PRODUCTS, strResponse);
		JSONObject obj = new JSONObject(strResponse);
		reponse.setResultCode(obj.getInt("resultCode"));
		reponse.setResultMsg(obj.getString("resultMsg"));
		if (reponse.getResultCode() == IBMSProtocol.STATUS_CODE_OK) {
			reponse.setCountPage(obj.getInt("countPage"));
			JSONArray jArray = obj.optJSONArray("data");
			ArrayList<AndroidNotificationsModel> arr = new ArrayList<AndroidNotificationsModel>();
			for (int i = 0; i < jArray.length(); i++) {
				JSONObject object = jArray.optJSONObject(i);
				AndroidNotificationsModel notificationsModel = parseNotifications(object);
				arr.add(notificationsModel);
			}
			reponse.setListAndroidNotificationsModels(arr);
		}
		return reponse;
	}

	private AndroidNotificationsModel parseNotifications(JSONObject object) {
		AndroidNotificationsModel notificationsModel = new AndroidNotificationsModel();
		String value = "";
		value = object.optString("Activity_id");
		notificationsModel.setActivity_id(value);
		value = object.optString("Activity");
		notificationsModel.setActivity(value);
		value = object.optString("Message");
		notificationsModel.setMessage(value);
		value = object.optString("Object_id");
		notificationsModel.setObject_id(value);
		return notificationsModel;
	}

	private ProductInfor parserProduct(JSONObject object) {
		// TODO Auto-generated method stub
		ProductInfor product = new ProductInfor();
		String value = "";
		value = object.optString("ID");
		product.setID(Utils.validateValue(value));

		value = object.optString("Name");
		product.setName(Utils.validateValue(value));

		value = object.optString("Title");
		product.setTitle(Utils.validateValue(value));

		value = object.optString("Intro");
		product.setIntro(Utils.validateValue(value));

		value = object.optString("Desc");
		product.setDesc(Utils.validateValue(value));

		value = object.optString("Image");
		product.setImage(Utils.validateValue(value));

		value = object.optString("Created_date");
		product.setCreateTime(Utils.validateValue(value));

		return product;
	}

	@Override
	public GetListSeminarResponse getListSeminar(GetListProductRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetListSeminarResponse response = new GetListSeminarResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_LIST_WORKSHOPS);
		requestObject.put("pageLimit", request.getPageLimit());
		requestObject.put("pageNumber", request.getPageNumber());

		Log.e(TAG + METHOD_GET_LIST_WORKSHOPS,
				"content=" + requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());

		Log.e(TAG + METHOD_GET_LIST_WORKSHOPS, strResponse);

		JSONObject object = new JSONObject(strResponse);
		response.setReturnCode(object.optInt("resultCode"));
		response.setCountPage(object.optInt("countPage"));

		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONArray jArray = object.optJSONArray("data");
			if (jArray == null) {
				return response;
			}
			ArrayList<Seminar> seminars = new ArrayList<Seminar>();
			for (int i = 0; i < jArray.length(); i++) {
				JSONObject obj = jArray.getJSONObject(i);
				Seminar seminar = parseSeminar(obj);
				seminars.add(seminar);

			}
			response.setListSeminars(seminars);
		}
		return response;
	}

	private Seminar parseSeminar(JSONObject obj) {
		// TODO Auto-generated method stub
		Seminar seminar = new Seminar();
		String value = "";
		value = obj.optString("ID");
		seminar.setID(Utils.validateValue(value));

		value = obj.optString("Name");
		seminar.setName(Utils.validateValue(value));

		value = obj.optString("Title");
		seminar.setTitle(Utils.validateValue(value));

		value = obj.optString("Intro");
		seminar.setIntro(Utils.validateValue(value));

		value = obj.optString("Desc");
		seminar.setDesc(Utils.validateValue(value));

		value = obj.optString("Image");
		seminar.setImage(Utils.validateValue(value));

		value = obj.optString("Created_date");
		seminar.setCreateTime(Utils.validateValue(value));

		value = obj.optString("Duration_from");
		seminar.setDurationFrom(Utils.validateValue(value));

		value = obj.optString("Duration_to");
		seminar.setDurationTo(Utils.validateValue(value));

		return seminar;
	}

	@Override
	public GetDetailProductBMSResponse getDetailBMS(
			GetDetailProductBMSRequest request) throws JSONException,
			ClientProtocolException, IOException {
		GetDetailProductBMSResponse response = new GetDetailProductBMSResponse();

		JSONObject requestObject = new JSONObject();
		switch (request.getType()) {
		case IBMSConfig.KEY_TYPE_DETAIL_PRODUCT:
			requestObject.put("actionType", METHOD_GET_DETAIL_PRODUCTS);
			break;
		case IBMSConfig.KEY_TYPE_DETAIL_PROMOTION:
			requestObject.put("actionType", METHOD_GET_DETAIL_PROMOTION);
			break;
		case IBMSConfig.KEY_TYPE_DETAIL_EDUCATION:
			requestObject.put("actionType", METHOD_GET_DETAIL_EDUCATION);
			break;
		case IBMSConfig.KEY_TYPE_DETAIL_SEMINAR:
			requestObject.put("actionType", METHOD_GET_DETAIL_SEMINAR);
			break;
		}
		requestObject.put("ID", request.getId());

		Log.e(TAG, "content=" + requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());

		Log.e(TAG, strResponse);

		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.optInt("resultCode"));

		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONObject ads = responseObj.optJSONObject("data");
			if (ads == null) {
				return response;
			}
			DetailBMSProduct product = parserDetailBMSProduct(ads);
			response.setDetailBMSProduct(product);
		}
		return response;
	}

	private DetailBMSProduct parserDetailBMSProduct(JSONObject obj) {
		// TODO Auto-generated method stub
		DetailBMSProduct product = new DetailBMSProduct();
		String value = "";
		value = obj.optString("ID");
		product.setID(Utils.validateValue(value));

		value = obj.optString("Name");
		product.setName(Utils.validateValue(value));

		value = obj.optString("Title");
		product.setTitle(Utils.validateValue(value));

		value = obj.optString("Intro");
		product.setIntro(Utils.validateValue(value));

		value = obj.optString("Desc");
		product.setDesc(Utils.validateValue(value));

		value = obj.optString("Image");
		product.setImage(Utils.validateValue(value));

		value = obj.optString("Created_date");
		product.setCreateTime(Utils.validateValue(value));

		value = obj.optString("Duration_from");
		product.setDurationFrom(Utils.validateValue(value));

		value = obj.optString("Duration_to");
		product.setDurationTo(Utils.validateValue(value));

		return product;
	}

	@Override
	public SendToAdminResponse sendToAdmin(SendToAdminRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		SendToAdminResponse response = new SendToAdminResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_SEND_TO_ADMIN);
		requestObject.put(AUTHENKEY, request.getAuthKey());
		requestObject.put("Content", request.getContent());
		requestObject.put("Title", request.getTittle());
		requestObject.put("Group_id", request.getGroup_Id());
		Log.e(TAG + METHOD_SEND_TO_ADMIN, "content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + METHOD_SEND_TO_ADMIN, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));

		return response;
	}

	@Override
	public GetSupportResponse getAdminInfo(GetSupportRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetSupportResponse response = new GetSupportResponse();
		JSONObject obj = new JSONObject();
		obj.put("actionType", METHOD_ADMIN_INFOR);
		obj.put(AUTHENKEY, request.getAuthenKey());
		Log.e(TAG + METHOD_ADMIN_INFOR, "content=" + obj.toString());
		String strResponse = getDataServerPost(URL, "content=" + obj.toString());
		Log.e(TAG + METHOD_ADMIN_INFOR, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		JSONObject objData = responseObj.optJSONObject("data");
		String value = "";
		EditAdminModel admin = new EditAdminModel();
		value = responseObj.optString("ID");
		admin.setID(Utils.validateValue(value));
		value = objData.optString("UserName");
		admin.setUserName(Utils.validateValue(value));
		value = objData.optString("FullName");
		admin.setFullName(Utils.validateValue(value));
		value = objData.optString("Email");
		admin.setEmail(Utils.validateValue(value));
		value = objData.optString("Sex");
		admin.setSex(Utils.validateValue(value));
		value = objData.optString("Skype");
		admin.setSkype(Utils.validateValue(value));
		value = objData.optString("Kakao");
		admin.setKakao(Utils.validateValue(value));
		value = objData.optString("Viber");
		admin.setViber(Utils.validateValue(value));
		value = objData.optString("Phone");
		admin.setPhone(Utils.validateValue(value));
		value = objData.optString("Mobile");
		admin.setMobile(Utils.validateValue(value));
		value = objData.optString("Desc");
		admin.setExtraInfo(Utils.validateValue(Html.fromHtml(value).toString()));
		value = objData.optString("Group_id");
		admin.setGroup_id(Utils.validateValue(value));
		value = objData.optString("CompanyName");
		admin.setNameCompany(Utils.validateValue(value));
		value = objData.optString("Address");
		admin.setAddressCompany(Utils.validateValue(value));
		value = objData.optString("Group_name");
		admin.setGroup_name(Utils.validateValue(value));
		response.setResultMessage(responseObj.optString("resultMsg"));
		response.setReturnCode(responseObj.optInt("resultCode"));
		response.setAdmin(admin);
		return response;
	}

	@Override
	public GetDetailReportbyTypeResponse getDetailReportbyType(
			GetDetailReportByTypeRequest request) throws JSONException,
			ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetDetailReportbyTypeResponse response = new GetDetailReportbyTypeResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_DETAIL_REPORT_BY_TYPE);
		requestObject.put("Type", request.getType());
		requestObject.put("Object_id", new JSONArray(request.getObjId()));
		requestObject.put("SortBy", request.getSortBy());
		requestObject.put("DateStart",
				Utils.convertDateReport(request.getDateStart()));
		requestObject.put("DateEnd",
				Utils.convertDateEndReport(request.getDateEnd()));
		requestObject.put("AuthKey", request.getAuthKey());

		Log.e(TAG, METHOD_GET_DETAIL_REPORT_BY_TYPE + "content="
				+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());

		Log.e(TAG, METHOD_GET_DETAIL_REPORT_BY_TYPE + strResponse);

		JSONObject object = new JSONObject(strResponse);
		response.setReturnCode(object.optInt("resultCode"));
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONArray jArray = object.optJSONArray("data");
			if (jArray == null) {
				return response;
			}
			if (request.getType() == 1) {
				ArrayList<DetailReportHospitalModel> arrayList = new ArrayList<DetailReportHospitalModel>();
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject obj = jArray.getJSONObject(i);
					DetailReportHospitalModel model = parserDetailReportHospital(obj);
					arrayList.add(model);

				}
				response.setListHospital(arrayList);
			} else {
				ArrayList<DetailReportDoctorModel> arrayList = new ArrayList<DetailReportDoctorModel>();
				for(int i=0;i<jArray.length();i++){
					JSONObject obj= jArray.getJSONObject(i);
					DetailReportDoctorModel model=parserDetailReportDoctor(obj);
					arrayList.add(model);
				}
				
				 
				response.setListDoctor(arrayList);
			}
		}
		
		return response;
	}

	private DetailReportDoctorModel parserDetailReportDoctor(JSONObject obj) {
		// TODO Auto-generated method stub
		DetailReportDoctorModel doctor = new DetailReportDoctorModel();
		String value = "";
		int intValue = 0;
		value = obj.optString("UserID");
		doctor.setDoctorId(Utils.validateIntValue(Integer.parseInt(value)));
		value = obj.optString("FullName");
		doctor.setDoctorName(Utils.validateValue(value));
		value = obj.optString("OperationCount");
		doctor.setOperationCount(Utils.validateValue(value));
		value = obj.optString("Revenue");
		doctor.setInComeDoctor(Utils.validateValue(value));

		return doctor;
	}

	private DetailReportHospitalModel parserDetailReportHospital(JSONObject obj) {
		// TODO Auto-generated method stub
		DetailReportHospitalModel hospital = new DetailReportHospitalModel();
		String value = "";
		value = obj.optString("Revenue");
		hospital.setRevenue(Utils.validateValue(value));

		value = obj.optString("OperationCount");
		hospital.setOperationCount(Utils.validateValue(value));

		value = obj.optString("HospitalName");
		hospital.setHospitalName(Utils.validateValue(value));

		value = obj.optString("HospitalID");
		hospital.setHospitalID(Utils.validateValue(value));

		value = obj.optString("CaseName");
		hospital.setCaseName(Utils.validateValue(value));

		value = obj.optString("CaseID");
		hospital.setCaseID(Utils.validateValue(value));

		return hospital;
	}

	@Override
	public GetDetailReportFileResponse getDetailReportFile(
			GetDetailReportFileRequest request) throws JSONException,
			ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetDetailReportFileResponse response = new GetDetailReportFileResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_DETAIL_REPORT_FILE);
		requestObject.put("Type", request.getType());
		requestObject.put("Company_id", new JSONArray(request.getCompanyId()));
		if (request.getCaseId() != null) {
			requestObject.put("Case_id", new JSONArray(request.getCaseId()));
		}
		requestObject.put("DateStart",
				Utils.convertDateReport(request.getDateStart()));
		requestObject.put("DateEnd",
				Utils.convertDateEndReport(request.getDateEnd()));
		requestObject.put("AuthKey", request.getAuthKey());

		Log.e(TAG,
				METHOD_GET_DETAIL_REPORT_FILE + "content="
						+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());

		Log.e(TAG, METHOD_GET_DETAIL_REPORT_FILE + strResponse);

		JSONObject object = new JSONObject(strResponse);
		response.setReturnCode(object.optInt("resultCode"));
		response.setResultMsg(object.getString("resultMsg"));

		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			response.setLink(object.getString("Link"));
		}

		return response;
	}

	@Override
	public GetComDoctorResponse getComDoctor(GetComDoctorRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetComDoctorResponse response = new GetComDoctorResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_GET_COM_DOCTOR);
		requestObject.put(AUTHENKEY, request.getAuthenKey());
		requestObject.put("Date", request.getDate());
		requestObject.put("Company_id", request.getComId());
		Log.e(TAG + METHOD_GET_COM_DOCTOR,
				"content=" + requestObject.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());
		Log.e(TAG + METHOD_GET_COM_DOCTOR, strResponse);
		JSONObject object = new JSONObject(strResponse);
		JSONArray jArray = object.optJSONArray("data");
		ArrayList<InfoDoctorEntity> doctors = new ArrayList<InfoDoctorEntity>();
		for (int i = 0; i < jArray.length(); i++) {
			JSONObject obj = jArray.getJSONObject(i);
			InfoDoctorEntity doctor = new InfoDoctorEntity();
			doctor = parserDoctor(obj);
			doctors.add(doctor);

		}
		response.setListDocttor(doctors);
		response.setResultMsg(object.optString("resultMsg"));
		response.setReturnCode(object.optInt("resultCode"));

		return response;
	}

	private InfoDoctorEntity parserDoctor(JSONObject obj) {
		// TODO Auto-generated method stub
		InfoDoctorEntity doctor = new InfoDoctorEntity();
		String value = "";
		int intValue = 0;
		value = obj.optString("ID");
		doctor.setComDoctorId(Utils.validateValue(value));
		value = obj.optString("User_id");
		doctor.setId(Utils.validateValue(value));
		value = obj.optString("Company_id");
		doctor.setCompany_Id(Utils.validateValue(value));
		value = obj.optString("Fullname");
		doctor.setNameDoctor(Utils.validateValue(value));
		value = obj.optString("Operation_count");
		doctor.setCoutSchedule(value);
		value = obj.optString("Revenue");
		doctor.setRevenue(value);

		return doctor;
	}

	@Override
	public SendUpdateComDoctorResponse sendUpdateComDoctor(
			SendUpdateComDoctorRequest request) throws JSONException,
			ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		SendUpdateComDoctorResponse response = new SendUpdateComDoctorResponse();
		JSONObject requestObj = new JSONObject();
		requestObj.put("actionType", METHOD_SEND_COM_DOCTOR);
		requestObj.put(AUTHENKEY, request.getAuthKey());
		requestObj.put("Date", request.getDate());
		Collection<JSONObject> arrayListObj = new ArrayList<JSONObject>();
		for (int i = 0; i < request.getData().size(); i++) {
			JSONObject object = new JSONObject();
			object.put("2", request.getData().get(i).getKeyID());
			object.put("1", request.getData().get(i).getSumInput());
			object.put("0", request.getData().get(i).getSumMedicalRecord());
			arrayListObj.add(object);
		}
		JSONArray array = new JSONArray(arrayListObj);
		requestObj.put("Data", array);
		requestObj.put("Date", request.getDate());
		String restual = requestObj.toString();
		Log.e(TAG + METHOD_SEND_COM_DOCTOR, "content=" + requestObj.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObj.toString());
		Log.e(TAG + METHOD_SEND_COM_DOCTOR, strResponse);
		JSONObject obj = new JSONObject(strResponse);
		response.setResultMsg(obj.optString("resultMsg"));
		response.setReturnCode(obj.optInt("resultCode"));
		return response;
	}



	@Override
	public GetListComHospitalResponse getListComHos(
			GetListComHospitalRequest request) throws JSONException,
			ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetListComHospitalResponse response = new GetListComHospitalResponse();
		JSONObject requestObj = new JSONObject();
		requestObj.put("actionType", METHOD_GET_LIST_COM_HOS);
		requestObj.put("Company_id", request.getComId());
		requestObj.put("Date", request.getDate());
		requestObj.put(AUTHENKEY, request.getAuthenKey());
		Log.e(TAG + METHOD_GET_LIST_COM_HOS, "content=" + requestObj.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObj.toString());
		Log.e(TAG + METHOD_GET_LIST_COM_HOS, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		ArrayList<Hospital> arrHospital = new ArrayList<Hospital>();
		JSONArray jArray = responseObj.optJSONArray("data");
		for (int i = 0; i < jArray.length(); i++) {
			JSONObject obj = jArray.getJSONObject(i);
			Hospital hospital = parserComHospital(obj);
			arrHospital.add(hospital);
		}
		response.setListComHospital(arrHospital);
		response.setResultMsg(responseObj.optString("resultMsg"));
		response.setReturnCode(responseObj.optInt("resultCode"));
		return response;
	}

	private Hospital parserComHospital(JSONObject obj) {
		Hospital hospital = new Hospital();
		String value = "";
		value = obj.optString("ID");
		hospital.setID(Utils.validateValue(value));
		value = obj.optString("Hospital_id");
		hospital.setId(Utils.validateValue(value));
		value = obj.optString("Hospital_name");
		hospital.setName(Utils.validateValue(value));
		value = obj.optString("Revenue");
		hospital.setRevenue(Utils.validateValue(value));
		return hospital;
	}

	@Override
	public GetListMedicalRecordHospitalResponse getListRecordHos(
			GetListMedicalRecordHospitalRequest request) throws JSONException,
			ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetListMedicalRecordHospitalResponse response = new GetListMedicalRecordHospitalResponse();
		JSONObject requestObj = new JSONObject();
		requestObj.put("actionType", METHOD_GET_LIST_RECORD_HOS);
		requestObj.put(AUTHENKEY, request.getAuthenKey());
		requestObj.put("Date", request.getDate());
		requestObj.put("Company_id", request.getCompany_id());
		requestObj.put("Hospital_id", request.getHospital_id());
		Log.e(TAG + METHOD_GET_LIST_RECORD_HOS,
				"content=" + requestObj.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObj.toString());
		Log.e(TAG + METHOD_GET_LIST_RECORD_HOS, strResponse);
		JSONObject object = new JSONObject(strResponse);
		JSONArray jsonArray = object.optJSONArray("data");
		ArrayList<MedicalRecordModel> records = new ArrayList<MedicalRecordModel>();
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject obj = jsonArray.getJSONObject(i);
			MedicalRecordModel record = parserRecordsHos(obj);
			records.add(record);
		}
		response.setRecords(records);
		response.setResultMsg(object.optString("resultMsg"));
		response.setReturnCode(object.optInt("resultCode"));
		return response;
	}

	private MedicalRecordModel parserRecordsHos(JSONObject obj) {
		// TODO Auto-generated method stub

		MedicalRecordModel record = new MedicalRecordModel();
		String value = "";
		int intValue = 0;
		value = obj.optString("ID");
		record.setId(Utils.validateValue(value));
		value = obj.optString("Name");
		record.setNameRecord(Utils.validateValue(value));
		value = obj.optString("Comp_hosp_ope_id");
		record.setHospital_id(Utils.validateValue(value));

		value = obj.optString("Operation_count");
		record.setOperatin_count(Utils.validateValue(value));

		return record;
	}

	@Override
	public SendUpdateHospitalRecordResponse sendUpdateHosRecord(
			SendUpdateHospitalRecordRequest request) throws JSONException,
			ClientProtocolException, IOException {
		// TODO Auto-generated method stub

		SendUpdateHospitalRecordResponse response = new SendUpdateHospitalRecordResponse();
		JSONObject requestObj= new JSONObject();
		requestObj.put("actionType", METHOD_SEND_HOS_RECORD);
		requestObj.put(AUTHENKEY, request.getAuthenKey());
		requestObj.put("Comp_hosp_id", request.getCom_hos_id());
		requestObj.put("Revenue", request.getRevenue());
		Collection<JSONArray>DataArray= new ArrayList<JSONArray>();
		for(int i=0;i<request.getDataRecord().size();i++){
			JSONArray array= new JSONArray();
			array.put(request.getDataRecord().get(i).getData());
			array.put(request.getDataRecord().get(i).getKeyID());
			DataArray.add(array);
		}
		JSONArray jArray= new JSONArray(DataArray);


		requestObj.put("CaseData", jArray);
		requestObj.put("Date", request.getDate());
		Log.e(TAG+METHOD_SEND_HOS_RECORD,"content="+requestObj.toString() );
		String strResponse=getDataServerPost(URL, "content="+requestObj.toString());
		Log.e(TAG+METHOD_SEND_HOS_RECORD,strResponse );
		JSONObject obj= new JSONObject(strResponse);
		response.setReturnCode(obj.optInt("resultCode"));
		response.setResultMsg(obj.getString("resultMsg"));
		return response;
	}

	@Override
	public SetReadNotifyResponse sendReadNotify(SetReadNotifyRequest request)
			throws JSONException, ClientProtocolException, IOException {
		SetReadNotifyResponse response = new SetReadNotifyResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_SEND_READ_NOTIFY);
		requestObject.put("AuthKey", request.getAuthKey());
		requestObject.put("ID", request.getId());

		Log.i(TAG,
				"Request " + METHOD_SEND_READ_NOTIFY + " content="
						+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());

		Log.i(TAG, "Respone " + METHOD_SEND_READ_NOTIFY + strResponse.toString());
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));

		return response;
	}

	@Override
	public int GetTimeNotification() throws JSONException,
	ClientProtocolException, IOException {
		int timeConnect = 0;
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_TIME_NOYIFICATION);
		String strResponse = getDataServerPost(URL,"content=" + requestObject.toString());
		timeConnect = Integer.parseInt(strResponse);
		return timeConnect;
	}

	@Override
	public SetReadMessageResponse sendReadMessage(SetReadMessageRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		SetReadMessageResponse response = new SetReadMessageResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_SEND_READ_MESSAGER);
		requestObject.put("AuthKey", request.getAuthkey());
		requestObject.put("ID", request.getId());

		Log.i(TAG,
				"Request " + METHOD_SEND_READ_MESSAGER + " content="
						+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());

		Log.i(TAG, "Respone " + METHOD_SEND_READ_MESSAGER + strResponse.toString());
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResultMsg(responseObj.getString("resultMsg"));

		return response;
	}

	@Override
	public SendDeleteNotifyResponse sendDeleteNotify(
			SendDeleteNotifyRequest request) throws JSONException,
			ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		SendDeleteNotifyResponse response = new SendDeleteNotifyResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_SEND_DELETE_NOTIFY);
		requestObject.put("AuthKey", request.getAuthKey());
		requestObject.put("ListID", new JSONArray(request.getListId()));

		Log.i(TAG,
				"Request " + METHOD_SEND_DELETE_NOTIFY + " content="
						+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());

		Log.i(TAG, "Respone " + METHOD_SEND_DELETE_NOTIFY + strResponse.toString());
		JSONObject responseObj = new JSONObject(strResponse);
		//		response.setReturnCode(responseObj.getInt("resultCode"));
		//		response.setResultMsg(responseObj.getString("resultMsg"));
		response.setReturnCode(IBMSProtocol.STATUS_CODE_OK);
		return response;
	}

	@Override
	public SendDeleteMessagerResponse sendDeleteMessager(
			SendDeleteMessagerRequest request) throws JSONException,
			ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		SendDeleteMessagerResponse response = new SendDeleteMessagerResponse();
		JSONObject requestObject = new JSONObject();
		requestObject.put("actionType", METHOD_SEND_DELETE_MESSAGER);
		requestObject.put("AuthKey", request.getAuthKey());
		requestObject.put("ListID", new JSONArray(request.getListId()));

		Log.i(TAG,
				"Request " + METHOD_SEND_DELETE_MESSAGER + " content="
						+ requestObject.toString());

		String strResponse = getDataServerPost(URL,
				"content=" + requestObject.toString());

		Log.i(TAG, "Respone " + METHOD_SEND_DELETE_MESSAGER + strResponse.toString());
		JSONObject responseObj = new JSONObject(strResponse);
		//		response.setReturnCode(responseObj.getInt("resultCode"));
		//		response.setResultMsg(responseObj.getString("resultMsg"));
		response.setReturnCode(IBMSProtocol.STATUS_CODE_OK);

		return response;
	}

	@Override
	public GetMessgerTwoGroupsResponse GetMessgerHome(
			GetMessegerTwoGroupsRequest request) throws JSONException,
			ClientProtocolException, IOException {
		GetMessgerTwoGroupsResponse response = new GetMessgerTwoGroupsResponse();
		JSONObject requestObj = new JSONObject();
		requestObj.put("actionType", METHOD_GET_MSG_TWO_GROUPS);
		requestObj.put("ObjectID", request.getObjID());
		requestObj.put("AuthKey", request.getAuthKey());
		requestObj.put("pageLimit", request.getPageLimit());
		requestObj.put("pageNumber", request.getPageNumber());
		
		Log.e(TAG + METHOD_GET_MSG_TWO_GROUPS, "content=" + requestObj.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObj.toString());
		Log.e(TAG + METHOD_GET_MSG_TWO_GROUPS, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			response.setPageCount(responseObj.getInt("countPage"));
			JSONArray ads = responseObj.optJSONArray("data");
			if (ads == null) {
				return response;
			}
			ArrayList<InfoMessegerModel> arrayList = new ArrayList<InfoMessegerModel>();
			for (int i = 0; i < ads.length(); i++) {
				JSONObject adJO = ads.optJSONObject(i);
				InfoMessegerModel model = parserMessegerModel(adJO);
				arrayList.add(model);
			}
			response.setArrMsg(arrayList);
		}
		return response;
		
	}

	@Override
	public SendMsgtoGroupsResponse SendMsgtoGroups(
			SendMsgtoGroupsRequest request) throws JSONException,
			ClientProtocolException, IOException {
		SendMsgtoGroupsResponse response = new SendMsgtoGroupsResponse();
		JSONObject requestObj = new JSONObject();
		requestObj.put("actionType", METHOD_SEND_MSG_TO_GROUPS);
		requestObj.put("Content", request.getContent());
		requestObj.put("Title", request.getTitle());
		requestObj.put("Group_id", request.getGroupId());
		requestObj.put("AuthKey", request.getAuthKey());
		
		Log.e(TAG + METHOD_SEND_MSG_TO_GROUPS, "content=" + requestObj.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObj.toString());
		Log.e(TAG + METHOD_SEND_MSG_TO_GROUPS, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResuiltMsg(responseObj.getString("resultMsg"));
		response.setIdMsg(responseObj.getString("ID"));
		return response;
	}

	@Override
	public GetListHintsResponse GetListHints(GetListHintsRequest request)
			throws JSONException, ClientProtocolException, IOException {
		// TODO Auto-generated method stub
		GetListHintsResponse response = new GetListHintsResponse();
		JSONObject requestObj = new JSONObject();
		requestObj.put("actionType", METHOD_GET_LIST_HINTS);
		requestObj.put("AuthKey", request.getAuthKey());
		Log.e(TAG + METHOD_GET_LIST_HINTS, "content=" + requestObj.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObj.toString());
		Log.e(TAG + METHOD_GET_LIST_HINTS, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResuiltMsg(responseObj.getString("resultMsg"));
		if (response.getReturnCode() == IBMSProtocol.STATUS_CODE_OK) {
			JSONArray ads = responseObj.optJSONArray("data");
			if (ads == null) {
				return response;
			}
			ArrayList<MsgHints> arrayList = new ArrayList<MsgHints>();
			for (int i = 0; i < ads.length(); i++) {
				JSONObject adJO = ads.optJSONObject(i).optJSONObject("MessageTemplate");
				MsgHints model = parserMessegerHints(adJO);
				arrayList.add(model);
			}
			response.setArrHints(arrayList);
		}
		return response;
	}

	private MsgHints parserMessegerHints(JSONObject object) {
		MsgHints model = new MsgHints();
		String value = "";
		value = object.optString("ID");
		model.setId(Utils.validateValue(value));
		value = object.optString("Content");
		model.setContent(Utils.validateValue(value));
		value = object.optString("Title");
		model.setTitle(Utils.validateValue(value));
		return model;
	}

	@Override
	public DeleteMsgResponse DeleteMsg(DeleteMsgRequest request)
			throws JSONException, ClientProtocolException, IOException {
		DeleteMsgResponse response = new DeleteMsgResponse();
		JSONObject requestObj = new JSONObject();
		requestObj.put("actionType", METHOD_DELETE_MESSEGER);
		requestObj.put("AuthKey", request.getAuthKey());
		requestObj.put("ID", request.getId());
		Log.e(TAG + METHOD_DELETE_MESSEGER, "content=" + requestObj.toString());
		String strResponse = getDataServerPost(URL,
				"content=" + requestObj.toString());
		Log.e(TAG + METHOD_DELETE_MESSEGER, strResponse);
		JSONObject responseObj = new JSONObject(strResponse);
		response.setReturnCode(responseObj.getInt("resultCode"));
		response.setResuiltMsg(responseObj.getString("resultMsg"));
		return response;
	}

}
